package com.duowan.realtime.scheduled.batch.reader.hyperLog;

import org.springframework.batch.item.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * 从hyerLog中读取数据，一次job执行一次。
 * 第二次执行的时候，返回null，则该job会complete
 *
 * @author tangsicheng
 * @version 1.0
 * @since 1.0
 */
public class HyperLogBatchReader extends HyperLogReader implements ItemStreamReader<List<Map>>, InitializingBean {

    private String kpiCode;
    private String hyperLogGroup;


    public void setHyperLogGroup(String hyperLogGroup) {
        this.hyperLogGroup = hyperLogGroup;
    }


    public void setKpiCode(String kpiCode) {
        this.kpiCode = kpiCode;
    }


    private List<Map> resultMapList = null;


    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(kpiCode, "kpiCode is empty");
        Assert.hasText(hyperLogGroup, "hyperLogGroup is empty");
    }

    @Override
    public List<Map> read() throws UnexpectedInputException, ParseException, NonTransientResourceException {
        return resultMapList;
    }

    @Override
    public void afterRead(Object item) {
        resultMapList = null;
    }

    @Override
    public void open(ExecutionContext executionContext) throws ItemStreamException {
        resultMapList = readFromHyperLog(hyperLogGroup, kpiCode);
    }


    @Override
    public void update(ExecutionContext executionContext) throws ItemStreamException {

    }

    @Override
    public void close() throws ItemStreamException {

    }

}
